home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / joerg2plot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  3.6 KB  |  199 lines

  1. /*
  2. */
  3.  
  4. #define _MAXSPCLEN  32768
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9. #ifndef TRUE
  10. #define TRUE 1L
  11. #endif
  12. #ifndef FALSE
  13. #define FALSE 0L
  14. #endif
  15.  
  16. float x,y,*spc,*tim;
  17.  
  18. help()
  19. {
  20.   printf("BAM2plot spectrum [-b2p] [-p2b]\n");
  21.   printf("  converts spectra from BAM to MULTIPLOT and vice versa\n");
  22.   printf("  options:\n");
  23.   printf("     -b2p     convert from BAM to MULTIPLOT\n");
  24.   printf("     -p2b     convert from MULTIPLOT to BAM\n");
  25.   return(0);
  26. }
  27.  
  28. main(argc,argv)
  29. int argc;
  30. char *argv[];
  31. {
  32. int n,i,max;
  33. char z[80];
  34.  
  35.  
  36.    spc= (float *)calloc(_MAXSPCLEN,sizeof(float));
  37.    tim= (float *)calloc(_MAXSPCLEN,sizeof(float));
  38.    if(tim==NULL) {
  39.       printf("sorry, not enough memory\n");
  40.       exit(-1);
  41.    }
  42.  
  43.    if(argc < 3) {
  44.       help();
  45.       exit(0);
  46.    }
  47.  
  48.    if(checkopt(argc,argv,"-b2p",z)) {
  49.       max=readspec(argv[1],spc,tim);
  50.       strcpy(z,argv[1]); n = strlen(z);
  51.       for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
  52.       writeplot(z,spc,tim,max);
  53.    }
  54.  
  55.    if(checkopt(argc,argv,"-p2b",z)) {
  56.       max = readplot(argv[1],spc,tim);
  57.       strcpy(z,argv[1]); n = strlen(z);
  58.       for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
  59.       writespec(z,spc,tim,max);
  60.    }
  61.    free(spc); free(tim);
  62.    exit(0);
  63. }
  64.  
  65. writeplot(name,spc,tim,max)
  66. char *name;
  67. float *spc, *tim;
  68. int max;
  69. {
  70. FILE *fp;
  71. int n;
  72. char *s;
  73.  
  74.    s = (char *) malloc(256);
  75.  
  76.    sprintf(s,"%s.dat",name);
  77.    fp = fopen(s,"w");
  78.    if(fp == NULL) {
  79.       fprintf(stderr,"BAM2plot: could not open >%s< for write\n",s);
  80.       free(s);
  81.       return(0);
  82.    }
  83.    fprintf(fp,"*XLABEL* Time\n*LEGEND* %s\n",name);
  84.    for(n = 0; n < max; n++) {
  85.       fprintf(fp,"%f %f\n",tim[n],spc[n]);
  86.    }
  87.    fclose(fp);
  88.  
  89.    free(s);
  90. }
  91.  
  92. readplot(name,spc,tim)
  93. char *name;
  94. float *spc, *tim;
  95. {
  96. FILE *fp;
  97. int max;
  98. char *s, *z;
  99.  
  100.    s = (char *) malloc(256); z = (char *) malloc(256);
  101.  
  102.    strcpy(s,name);
  103.    fp = fopen(s,"r");
  104.    if(fp == NULL) {
  105.       strcat(s,".dat");
  106.       fp = fopen(s,"r");
  107.       if(fp == NULL) {
  108.          fprintf(stderr,"BAM2plot: could not open >%s< for read\n",name);
  109.          free(s); free(z);
  110.          return(0);
  111.       }
  112.    }
  113.    while(!feof(fp)) {
  114.       fgets(s,200,fp); sscanf(s,"%s",z);
  115.       if(strcmp(z,"*LEGEND*") == 0) break;
  116.    }
  117.    max = 0;
  118.    while(!feof(fp)) {
  119.       fgets(s,200,fp);
  120.       if(s[0] == '*') continue;
  121.       if(strlen(s) < 3) break;
  122.       sscanf(s,"%f %f\n",&tim[max],&spc[max]);
  123.       max = max + 1;
  124.    }
  125.    fclose(fp);
  126.  
  127.    free(s); free(z);
  128. }
  129.  
  130. readspec(name,spc,tim)
  131. char *name;
  132. float *spc, *tim;
  133. {
  134. FILE *fp;
  135. int max;
  136. double x;
  137. char *s, *z;
  138.  
  139.    s = (char *) malloc(256); z = (char *) malloc(256);
  140.  
  141.    strcpy(s,name);
  142.    fp = fopen(s,"r");
  143.    if(fp == NULL) {
  144.       fprintf(stderr,"BAM2plot: could not open >%s< for read\n",name);
  145.       free(s); free(z);
  146.       return(0);
  147.    }
  148.    max = 0;
  149.    while(!feof(fp)) {
  150.       fgets(s,200,fp);
  151.       if(strlen(s) == 0) break;
  152.       sscanf(s,"%lf",&x);
  153.       tim[max] = x;
  154.       fgets(s,200,fp);
  155.       if(strlen(s) == 0) break;
  156.       sscanf(s,"%lf",&x);
  157.       spc[max] = x;
  158.       max = max + 1;
  159.    }
  160.    fclose(fp);
  161.    free(s); free(z);
  162.    return(max);
  163. }
  164.  
  165. writespec(name,spc,tim,max)
  166. char *name;
  167. float *spc, *tim;
  168. int max;
  169. {
  170. FILE *fp;
  171. int n;
  172.  
  173.    fp = fopen(name,"w");
  174.    if(fp == NULL) {
  175.       fprintf(stderr,"BAM2plot: could not open >%s< for write\n",name);
  176.       return(0);
  177.    }
  178.    for(n = 0; n < max; n++) {
  179.       fprintf(fp,"%f\n%f\n",tim[n],spc[n]);
  180.    }
  181.    fclose(fp);
  182. }
  183.  
  184. checkopt(argc,argv,s,sv)
  185. int argc;
  186. char *argv[],s[],sv[];
  187. {
  188. int   n,erg;
  189.  
  190.    erg=FALSE;
  191.    for(n=1;n<argc;n++) {
  192.       if(strcmp(argv[n],s)==0) {
  193.          erg=TRUE;
  194.          if((n + 1) < argc) strcpy(sv,argv[n+1]);
  195.       }
  196.    }
  197.    return(erg);
  198. }
  199.